<template>
  <div class="padding-l-20 padding-r-20 padding-t-20 h-100 table-page">
    <filter-bar>
      <template slot="left-bar">
          <a-select
            class="select"
            v-model="searchForm.enabled"
            @change="page.currentPage = 1;getList(page, searchForm)"
          >
            <a-select-option :key="item.key" v-for="item in cardArr">{{
              item.title
            }}</a-select-option>
          </a-select>
      </template>
      <template slot="right-bar">
        <a-button
          @click="edit(0)"
          class="nbcs-btn nbcs-btn-primary position-r"
          style="top:-1.6px"
          >新增</a-button
        >
      </template>
    </filter-bar>
    <a-table
      :columns="columns"
      :data-source="list"
      :pagination="pageConfig"
      rowKey="id"
      :loading="loading"
      :scroll="{ x: 1000}"
    >
      <template slot="roles" slot-scope="text">
        <a-tag v-for="(item,index) in text" :key="index">{{item.name}}</a-tag>
      </template>
      <span slot="tags" slot-scope="text, record">
        <a-tag v-if="record.enabled" class="tag-green">
          正常
        </a-tag>
        <a-tag v-else class="tag-red">
          锁定
        </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="edit(1, record)">编辑</a>
        <a-divider type="vertical" />
        <a-popconfirm title="确定删除该用户吗？" @confirm="del(record)">
          <a-icon slot="icon" type="question-circle-o" style="color: red" />
          <a class="table-red-btn">删除</a>
        </a-popconfirm>
      </span>
    </a-table>
    <control-form ref="controlForm" @getList="getList(page, searchForm)"></control-form>
  </div>
</template>

<script>
import FilterBar from "@/layouts/FilterBar.vue";
import { list, del, resetPwd, lockUser } from "@/services/user";
import ControlForm from "./controlForm";
import {cutDateSecond} from '@/utils/date'
export default {
  name: "ControlCard",
  components: { FilterBar, ControlForm },
  data() {
    return {
      loading: false,
      cardArr: [
        { title: "控制卡品牌（全部）", key: "" },
        { title: "正常", key: 1 },
        { title: "锁定", key: 0 }
      ],
      searchForm: {
        realName: "",
        username: "",
        enabled: ""
      },
      list: [],
      columns: [
        { title: "控制卡品牌",dataIndex: "realName" },
        { title: "控制卡型号", dataIndex: "username" },
        { title: "最大分辨率", dataIndex: "phone" },
        {
          title: "生成时间",
          dataIndex: "createTime",
          customRender:text=>{
            return cutDateSecond(text)
          }},
        {
          title: "操作",
          key: "operation",
          fixed: "right",
          width: 140,
          scopedSlots: { customRender: "action" }
        }
      ],
      pageConfig: {
        showQuickJumper: true,
        showSizeChanger: true,
        defaultPageSize: 20,
        pageSizeOptions: ["20", "30", "50"],
        total: 0,
        current:1,
        showTotal: total => {
          return `共 ${total} 条`;
        }
      },
      page: {
        currentPage: 1,
        pageSize: 20
      }
    };
  },
  computed: {},
  created() {
    this.getList(this.page);
  },
  methods: {
    getList(page, params) {
      this.loading = true;
      list(
        Object.assign(
          {
            page: page.currentPage,
            pageSize: page.pageSize
          },
          params
        )
      ).then(res => {
        this.loading = false;
        const { data } = res.data;
        this.list = data.list;
        this.pageConfig.current = data.page;
        this.pageConfig.total = data.totalRow;
      });
    },
    del(obj) {
      del({ id: obj.id }).then(res => {
        if (res.data.code === 0) {
          this.$message.success("删除成功");
          this.getList(this.page);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //重置密码
    reset(id) {
      resetPwd({ id: id }).then(res => {
        if (res.data.code == 0) {
          this.$message.success("重置密码成功");
        }
      });
    },
    //解锁/锁定用户
    lock(enabled, id) {
      let flag = enabled ? 0 : 1;
      console.log(flag);
      lockUser({
        enabled: flag,
        id: id
      }).then(res => {
        if (res.data.code == 0) {
          this.$message.success("操作成功");
          this.getList(this.page);
        }
      });
    },
    //添加/编辑
    edit(action, data = {}) {
      this.$refs['controlForm'].changeForm(true,action,data);
    },
    //授权
    auth(data) {
      this.$bus.$emit("userAuthVisible", { state: true, data: data.id });
    }
  }
};
</script>

<style lang="less" scoped>
.select {
  width: 200px;
  /deep/ .ant-select-selection {
    width: 100%;
  }
}
._input {
  width: 180px;
}
</style>
